/*
 * Copyright (c) 2010-2011, okazoh_tk. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *  - Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *  - Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *  - Neither the name of the copyright holder nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "alkes/gfx/RectangleShape3DImpl.h"

namespace alkes {

RectangleShape3DImpl* RectangleShape3DImpl::create(IGraphics3DDevice* device, uint32_t num)
{
    AL_PRE_COND(device && num, 0);

    return new RectangleShape3DImpl(device, num);
}

void RectangleShape3DImpl::destroy()
{
    delete this;
}

RectangleShape3DImpl::RectangleShape3DImpl(IGraphics3DDevice* device, uint32_t num)
: RectangleShape(num)
, device_(device)
, vbuffer_(0)
{
}

RectangleShape3DImpl::~RectangleShape3DImpl()
{
}

IVertexBuffer* RectangleShape3DImpl::getVertexBuffer()
{
    if (!vbuffer_)
    {
        vbuffer_ = device_->createVertexBuffer(AL_VERTEX_XY|AL_VERTEX_COLOR);
    }

    return vbuffer_;
}

bool RectangleShape3DImpl::renderTo(IGraphics3DDevice* device, uint32_t start, uint32_t count)
{
    if (0 == count)
        return true;

    AL_PRE_COND(start < getArrayNum() && construct(device, getVertexBuffer()) && vbuffer_, false);

    uint32_t i;
    uint32_t num = start + count;

    if (getArrayNum() < num)
        num = getArrayNum();

    bool ret = true;
    PrimitiveType type;
    for (i = start; i < num; ++i)
    {
        if (isLine(i))
        {
            type = AL_PRIM_LINE_LOOP;
        }
        else
        {
            type = AL_PRIM_TRIANGLE_FAN;
        }

        ret = device->drawPrimitive(
            vbuffer_,
            type,
            i * 4, 4) && ret;
    }

    return ret;
}

bool RectangleShape3DImpl::construct(IGraphics3DDevice* /*device*/, IVertexBuffer* vbuffer)
{
    if (!isDirty())
        return true;

    AL_PRE_COND(vbuffer, false);

    uint32_t i;
    for (i = 0; i < getArrayNum(); ++i)
    {
        setVertex(vbuffer, i, getRectangle(i));
    }

    setDirty(false);

    return true;
}

void RectangleShape3DImpl::setVertex(IVertexBuffer* vbuffer, uint32_t index, const Rectangle& rectangle)
{
    Vector2D vec[4] = {
        {(float)rectangle.rect.x                       , (float)rectangle.rect.y                        },
        {(float)rectangle.rect.x + rectangle.rect.width, (float)rectangle.rect.y                        },
        {(float)rectangle.rect.x + rectangle.rect.width, (float)rectangle.rect.y + rectangle.rect.height},
        {(float)rectangle.rect.x                       , (float)rectangle.rect.y + rectangle.rect.height},
    };

    vbuffer->setVertex2D(index * 4, 4, vec);

    Color col[4] = {
        rectangle.color0, rectangle.color0, rectangle.color0, rectangle.color0,
    };

    if (rectangle.isGradient())
    {
        if (rectangle.isHorizontalGradient())
        {
            col[1] = rectangle.color1;
            col[2] = rectangle.color1;
        }
        else
        {
            col[2] = rectangle.color1;
            col[3] = rectangle.color1;
        }
    }

    vbuffer->setDiffuse(index * 4, 4, col);
}

}
